<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Topic;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Renderable\UserTable;

use App\Models\TopicCategory;

class TopicController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Topic(['user']), function (Grid $grid) {
            $grid->column('id');
            $grid->column('title')->limit(10);
            $grid->column('category_id')
                ->using(TopicCategory::query()->pluck('name', 'id')->toArray())
                ->label();
            $grid->column('user.name', '用户');
            $grid->column('body')->limit(50);
            $grid->column('reply_count');
            $grid->column('view_count');
            $grid->is_top()->switch();
            $grid->column('order')->sortable();
            /*$grid->column('excerpt');
            $grid->column('slug');*/
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Topic(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('category_id');
            $show->field('body');
            $show->field('user_id');
            $show->field('reply_count');
            $show->field('view_count');
            $show->field('last_reply_user_id');
            $show->field('order');
            $show->field('excerpt');
            $show->field('slug');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Topic(['user']), function (Form $form) {
            $form->display('id');
            $form->select('category_id')->options(TopicCategory::query()->pluck('name', 'id'))->required();
            $form->text('title');
            $form->editor('body');
            $form->selectTable('user_id')
                ->required()
                ->title('作品列表')
                ->dialogWidth('50%') // 弹窗宽度，默认 800px
                ->from(UserTable::make()) // 设置渲染类实例，并传递自定义参数
                ->model(\App\Models\User::class, 'id', 'name'); // 设置编辑数据显示
            $form->number('reply_count')->default(0);
            $form->number('view_count')->default(0);
            $form->switch('is_top');
            $form->number('order')->default(0);
            $form->text('excerpt')->default('');
            $form->text('slug')->default('');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
